const fs = require('fs');
const path = require('path');
const https = require('https');

// 确保图片目录存在
const imagesDir = path.join(__dirname, '../public/images');
if (!fs.existsSync(imagesDir)) {
  fs.mkdirSync(imagesDir, { recursive: true });
}

const images = [
  {
    name: 'calculator.jpg',
    url: 'https://images.unsplash.com/photo-1542601906990-b4d3fb778b09?w=800'
  },
  {
    name: 'challenges.jpg',
    url: 'https://images.unsplash.com/photo-1518173946687-a4c8892bbd9f?w=800'
  },
  {
    name: 'community.jpg',
    url: 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=800'
  },
  {
    name: 'progress.jpg',
    url: 'https://images.unsplash.com/photo-1516321165247-4aa89a48be28?w=800'
  }
];

// 下载图片
images.forEach(image => {
  const filePath = path.join(imagesDir, image.name);
  const file = fs.createWriteStream(filePath);

  https.get(image.url, response => {
    response.pipe(file);
    file.on('finish', () => {
      file.close();
      console.log(`下载完成: ${image.name}`);
    });
  }).on('error', err => {
    fs.unlink(filePath);
    console.error(`下载失败 ${image.name}:`, err.message);
  });
}); 